package service

import (
	"teacher-helper/dao"
	"teacher-helper/models"
	"teacher-helper/models/request"
)

func AddOneStuReward(StuReward models.SysStuReward) error {
	return dao.DB.Create(&StuReward).Error
}

func GetStuRewardList(queryInfo request.QueryInfo) (StuRewards []models.SysStuReward, total int, err error) {
	if queryInfo == (request.QueryInfo{}){
		err = dao.DB.Preload("Student").Preload("Reward").Find(&StuRewards).Count(&total).Error
		return
	}
	if queryInfo.Query == "" {
		if err = dao.DB.Model(&models.SysStuReward{}).Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Reward").Preload("Student").Find(&StuRewards).Error
	} else {
		if err = dao.DB.Model(&models.SysStuReward{}).Where("stu_id like ?", queryInfo.Query+"%").Count(&total).Error; err != nil {
			return
		}
		err = dao.DB.Where("stu_id like ?", queryInfo.Query+"%").Limit(queryInfo.PageSize).Offset((queryInfo.PageNum - 1) * queryInfo.PageSize).Preload("Reward").Preload("Student").Find(&StuRewards).Error
	}
	return
}

func UpdateStuReward(StuReward models.SysStuReward) error {
	return dao.DB.Save(&StuReward).Error
}

func DeleteStuReward(id int) error {
	return dao.DB.Where("id = ?", id).Delete(&models.SysStuReward{}).Error
}
